import tool from '../modules/tool'
const controls = tool.cControls
import { useStore } from '../modules/store'
const store = useStore()

export default async (refreshPage, handleError) => {

    const data = {
        list: [],
        async get() {
            await tool.flow(async () => {
                let re = await tool.requestData('/category/getList', {}, {}, 'get')
                if (re.status) {
                    this.list = re.data
                } else {
                    throw re.content
                }
            }, err => {
                tool.toast.error(`${err}`)
            })
        }
    }

    await data.get()

    const refresh = async () => {
        return {
            type: 1,
            main: {
                tab: [
                    {
                        title: '全部类别',
                        bottomControls: [],
                        topControls: [
                            controls.button({
                                title: '添加',
                                leftIcon: 'add',
                                type: 'primary',
                                callBack() {
                                    store.showFormDialog('添加类别', [], [
                                        {
                                            field: 'name',
                                            title: '名称',
                                            type: 'multipleText'
                                        }
                                    ], {}, false, (e) => {
                                        tool.flow(async () => {
                                            if (!e.name) throw '类别名称不能为空'
                                            if (e.name.length > 16) throw '类别不能长于16和字符'
                                            let re = await tool.requestData('/category/add', {}, {
                                                name: e.name
                                            }, 'post')
                                            if (re.status) {
                                                await data.get()
                                                refreshPage()
                                                store.hideFormDialog()

                                            } else {
                                                throw re.content
                                            }
                                        }, err => {
                                            tool.toast.error(`${err}`)
                                        })
                                    })
                                }
                            }),
                        ],
                        data: {
                            type: 'table',
                            enableSelect: false,
                            form: [
                                {
                                    field: 'name',
                                    title: '名称'
                                }
                            ],
                            list: (() => {
                                let list = tool.deepCopyObject(data.list)
                                for (let i = 0; i < list.length; i++) {
                                    list[i].op = [
                                        {
                                            title: '编辑',
                                            icon: 'edit',
                                            op: 'edit'
                                        },
                                        {
                                            title: '删除',
                                            icon: '删除',
                                            op: 'delete'
                                        },
                                    ]
                                }
                                return list
                            })(),
                            callBack: async (e) => {
                                if (e.op === 'delete') {
                                    store.showModal2('删除类别', `是否删除类别 ${e.item.name} ？`, () => {
                                        tool.flow(async () => {
                                            let re = await tool.requestData('/category/delete', {}, {
                                                id: e.item.id
                                            })
                                            if (re.status) {
                                                await data.get()
                                                refreshPage()
                                            } else {
                                                throw re.content
                                            }
                                        }, err => tool.toast.error(`${err}`))
                                    })
                                }
                                if (e.op === 'edit') {
                                    store.showFormDialog('编辑类别', [], [
                                        {
                                            field: 'name',
                                            title: '名称'
                                        },
                                    ],
                                        e.item,
                                        false,
                                        (e1) => {
                                            tool.flow(async () => {
                                                if (!e1.name) throw '类别名称不能为空'
                                                if (e1.name.length > 16) throw '类别不能长于16和字符'
                                                let re = await tool.requestData('/category/update', {}, {
                                                    id: e.item.id,
                                                    name: e1.name
                                                }, 'post')
                                                if (re.status) {
                                                    await data.get()
                                                    refreshPage()
                                                    store.hideFormDialog()
                                                } else {
                                                    throw re.content
                                                }
                                            }, err => {
                                                tool.toast.error(`${err}`)
                                            })
                                        }
                                    )
                                }
                            },
                        }
                    }
                ]
            }
        }
    }

    return refresh
}